package god530.com.simpleapp_slide.account.tabs.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import god530.com.simpleapp_slide.R;
import god530.com.simpleapp_slide.account.title.TitleBarUtils;
import god530.com.simpleapp_slide.account.tabs.account.model.AccountModel;
import god530.com.simpleapp_slide.account.tabs.account.presenter.AccountTypePresenter;
import god530.com.simpleapp_slide.account.tabs.account.view.IAccountView;

public class AccountActivity extends AppCompatActivity implements View.OnClickListener
    , IAccountView{

    private LinearLayout account_scroll_contents;
    private TextView account_total;
    private TextView account_totalprice;
    private TextView account_borrow;
    private TextView account_borrowprice;

    private AccountTypePresenter accountTypePresenter;
    private double sum; // 账户上的总金额

    public static void launch(Context ctx) {
        Intent intent = new Intent(ctx, AccountActivity.class);
        ctx.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题
//        getWindow().setFlags(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        setContentView(R.layout.activity_account);

        initView();
        accountTypePresenter = new AccountTypePresenter(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        accountTypePresenter.loadAccountModel();
    }

    public void initView() {
        // 这是标题栏
        TitleBarUtils titleBarUtils = new TitleBarUtils(findViewById(R.id.account_titlebar));
        titleBarUtils.setTitleView("账户");
        titleBarUtils.setLeftView(R.drawable.account_back);
        titleBarUtils.setRightImageView(R.drawable.account_add);
        titleBarUtils.setLeftViewClickListener(this);
        titleBarUtils.setRightViewClickListener(this);

        // 获取上面的标题等
        account_total = (TextView) findViewById(R.id.account_total);
        account_totalprice = (TextView) findViewById(R.id.account_totalprice);
        account_borrow = (TextView) findViewById(R.id.account_borrow);
        account_borrowprice = (TextView) findViewById(R.id.account_borrowprice);


        // 获取下边具体的内容。
        account_scroll_contents = (LinearLayout) findViewById(R.id.account_scroll_contents);
    }


    @Override
    public void setListAccountType(List<AccountModel> listAccountType) {
        int size = listAccountType.size();
        LayoutInflater from = getLayoutInflater().from(this);
        if(size <= 0) {
            String[] stringArray = getResources().getStringArray(R.array.default_account_type);
            for(int i = 0; i < stringArray.length; i++) {
                account_scroll_contents.addView(initAccountItem(new AccountModel("", stringArray[i], 0.00), from));
            }
        }
        account_scroll_contents.removeAllViews();
        for(int i = 0; i < size; i++) {
            // 添加多个条目
            final AccountModel accountModel = listAccountType.get(i);
            sum += accountModel.getMoney();
            account_scroll_contents.addView(initAccountItem(accountModel, from));
        }
        account_totalprice.setText(String.valueOf(sum));
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    /**
     *
     * @param accountModel
     * @param from
     * @return
     */
    private View initAccountItem(final AccountModel accountModel, LayoutInflater from) {
        View inflate = from.inflate(R.layout.account_content_item, null);
        ImageView account_item_iv = (ImageView) inflate.findViewById(R.id.account_item_iv);
        TextView account_item_tv_name = (TextView) inflate.findViewById(R.id.account_item_tv_name);
        TextView account_item_tv_price = (TextView) inflate.findViewById(R.id.account_item_tv_price);

        account_item_tv_name.setText(accountModel.getValue());
        account_item_tv_price.setText(String.valueOf(accountModel.getMoney()));

        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountDetailsActivity.launch(AccountActivity.this, accountModel);
            }
        });
        return inflate;
    }

//    public void addContents(List<AccountModel> listAccountType) {
//        int size = listAccountType.size();
//        LayoutInflater from = getLayoutInflater().from(this);
//        for(int i = 0; i < size; i++) {
//            // 添加多个条目
//            final AccountModel accountModel = listAccountType.get(i);
//
//            View inflate = from.inflate(R.layout.account_content_item, null);
//            ImageView account_item_iv = (ImageView) inflate.findViewById(R.id.account_item_iv);
//            TextView account_item_tv_name = (TextView) inflate.findViewById(R.id.account_item_tv_name);
//            TextView account_item_tv_price = (TextView) inflate.findViewById(R.id.account_item_tv_price);
//
//            account_item_tv_name.setText(accountModel.getValue());
//            account_item_tv_price.setText(String.valueOf(accountModel.getMoney()));
//
//            inflate.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    AccountDetailsActivity.launch(AccountActivity.this, accountModel);
//                }
//            });
//        }
//    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.title_right:
                Toast.makeText(this, "标题右边被点击了", Toast.LENGTH_SHORT).show();
                AccountDetailsActivity.launch(AccountActivity.this, AccountDetailsActivity.TYPE_UPDATE);
                break;
        }
    }

}
